package com.example.layui.demo.controller;

import com.example.layui.demo.entity.Article;
import com.example.layui.demo.entity.Article;
import com.example.layui.demo.entity.Customer;
import com.example.layui.demo.entity.Result;
import com.example.layui.demo.mapper.ArticleMapper;
import com.example.layui.demo.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MyController {


    @GetMapping("/abc")
    public String TestPage() {
        return "test";
    }

    @GetMapping("/")
    public String registerpage() {
        return "register";
    }

    @PostMapping("/register")
    @ResponseBody
    public Map<String, String> register(Customer customer) {
        System.out.println(customer);
        Map<String, String> map = new HashMap<>();
        map.put("msg", "你注册成功了");
        return map;
    }


    @GetMapping("/student")
    public String student() {
        return "stu";
    }

    @Autowired
    MyService myService;

    @GetMapping("/student1")
    @ResponseBody
    public Map<String, Result> student1() {
        Map<String, Result> map = new HashMap<>();
        Result result = myService.getCount();
        map.put("result", result);
        return map;
    }


    @GetMapping("/list")
    public String list() {
        return "list";
    }


    @GetMapping("/getList")
    @ResponseBody
    public Map<String, Object> getList(int page, int limit) {

        if (page == 0) {
            page = 1;
        }
        int start = (page - 1) * limit;
        Map<String, Integer> param = new HashMap<>();
        param.put("start", start);

        param.put("limit", limit);
        Map<String, Object> map = new HashMap<>();
        List<Article> articleList = myService.getArticleList(param);

//        "code": 0,
////                "msg": "...",
////                "count": 3,

        map.put("code", 0);

        map.put("msg", "请求成功");

        map.put("count", myService.getArticleCount());//总记录数

        map.put("data", articleList);
        return map;

    }


    @PostMapping("/getAticleByid/{id}")
    @ResponseBody
    public Map<String, Object> getAticleByID(@PathVariable("id") int id) {


        Map<String, Object> map = new HashMap<>();
        Article article = myService.getArticleByid(id);

//        "code": 0,
////                "msg": "...",
////                "count": 3,

        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", 1);//总记录数
        map.put("data", article);
        return map;

    }


    @PostMapping("/updateArticle")
    @ResponseBody
    public Map<String, Object> updateArticle(Article article) {


        Map<String, Object> map = new HashMap<>();
        Integer re = myService.updateArticle1(article);

//        "code": 0,
////                "msg": "...",
////                "count": 3,

        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", 1);//总记录数
        map.put("data", re);
        return map;

    }


    @GetMapping("deleteAByids")
    @ResponseBody
    public Map<String, Object> deleteAByids(Integer[] ids) {

        Map<String, Object> map = new HashMap<>();
        Integer re = null;
        // for (Integer i : ids) {
        re = myService.DeleteArticleByids(ids);
        //  }
        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", 1);//总记录数
        map.put("data", re);
        return map;
    }


}
